﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using Entity;
using System.Data.SqlClient;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;

namespace DAL
{
    public class BolsaWrapper : BaseWrapper, IWrapper<Bolsa>
    {
        public List<Bolsa> GetAll()
        {
            List<Bolsa> LISTA = new List<Bolsa>();

            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_ENDERECO_GETALL", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            ////Parametros
            //// cmd.Parameters.AddWithValue("@NomeCampo", value)


            try
            {
                cn.Open();
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    Bolsa bolsa = new Bolsa();
                     bolsa.IDBOLSA      =int.Parse(dr["IDBOLSA"	    ].ToString());
                     bolsa.IDESCOLA		=int.Parse(dr["IDESCOLA"		].ToString());
                     bolsa.VALIDA		=bool.Parse(dr["VALIDA"		].ToString());          
                     bolsa.VAGAS		=int.Parse(dr["VAGAS"		    ].ToString());      
                     bolsa.VALOR		=int.Parse(dr["VALOR"		    ].ToString());      
                     bolsa.TIPODESCONTO	=bool.Parse(dr["TIPODESCONTO"	].ToString());

                    LISTA.Add(bolsa);
                }
            }
            catch (SqlException ex)
            {
                Messages += "Erro na inserção de dados\n";
                Messages += ex.Message.ToString();
                }
            finally
            {
                ;
                cn.Close();
            }
            return LISTA;
        }

        public Bolsa GetById(int id)
        {
            Bolsa bolsa = new Bolsa();

            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_BOLSA_GETBYID", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            ////Parametros
            //// cmd.Parameters.AddWithValue("@NomeCampo", value)

            try
            {
                cn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {


                    
                    bolsa.IDBOLSA = int.Parse(dr["IDBOLSA"].ToString());
                    bolsa.IDESCOLA = int.Parse(dr["IDESCOLA"].ToString());
                    bolsa.VALIDA = bool.Parse(dr["VALIDA"].ToString());
                    bolsa.VAGAS = int.Parse(dr["VAGAS"].ToString());
                    bolsa.VALOR = int.Parse(dr["VALOR"].ToString());
                    bolsa.TIPODESCONTO = bool.Parse(dr["TIPODESCONTO"].ToString());

                }
            }
            catch (SqlException ex)
            {
                Messages += "Erro na inserção de dados\n";
                Messages += ex.Message.ToString();
            }
            finally
            {
                ;
                cn.Close();
            }
            return bolsa;
        }

        public bool Insert(Bolsa entity)
        {
            bool status = false;
            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_BOLSA_INSERT", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter parametro = new SqlParameter("@IDBOLSA", DbType.UInt32);
            parametro.Direction = ParameterDirection.Output;

            cmd.Parameters.Add(parametro);
     



            cmd.Parameters.AddWithValue("IDBOLSA", entity.IDBOLSA);
            cmd.Parameters.AddWithValue("IDESCOLA"		,entity.IDESCOLA		);
            cmd.Parameters.AddWithValue("VALIDA"		,entity.VALIDA		    );    
            cmd.Parameters.AddWithValue("VAGAS"		    ,entity.VAGAS		    );
            cmd.Parameters.AddWithValue("VALOR"		    ,entity.VALOR		    );
            cmd.Parameters.AddWithValue("TIPODESCONTO"	,entity.TIPODESCONTO	);

            try
            {
                cn.Open();
                if (cmd.ExecuteNonQuery() > 0)
                    status = true;
                entity.IDBOLSA = int.Parse(cmd.Parameters[0].Value.ToString());

            }
            catch (SqlException ex)
            {
                Messages += "Erro na inserção de dados\n";
                Messages += ex.Message.ToString();

            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
            }

            return status;

        }

        public bool Update(Bolsa entity)
        {
            bool status = false;
            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_BOLSA_UPDATE", cn);
            cmd.CommandType = CommandType.StoredProcedure;


            cmd.Parameters.AddWithValue("IDBOLSA", entity.IDBOLSA);
            cmd.Parameters.AddWithValue("IDESCOLA", entity.IDESCOLA);
            cmd.Parameters.AddWithValue("VALIDA", entity.VALIDA);
            cmd.Parameters.AddWithValue("VAGAS", entity.VAGAS);
            cmd.Parameters.AddWithValue("VALOR", entity.VALOR);
            cmd.Parameters.AddWithValue("TIPODESCONTO", entity.TIPODESCONTO);

            try
            {
                cn.Open();
                if (cmd.ExecuteNonQuery() > 0)
                    status = true;

            }
            catch (SqlException ex)
            {
                Messages += "Erro na inserção de dados\n";
                Messages += ex.Message.ToString();

            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
            }

            return status;

        }

        public bool Delete(Bolsa entity)
        {
            bool status = false;
            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_BOLSA_DELETE", cn);
            cmd.CommandType = CommandType.StoredProcedure;


            cmd.Parameters.AddWithValue("IDBOLSA", entity.IDBOLSA);

            try
            {
                cn.Open();
                if (cmd.ExecuteNonQuery() > 0)
                    status = true;

            }
            catch (SqlException ex)
            {
                Messages += "Erro na inserção de dados\n";
                Messages += ex.Message.ToString();

            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
            }

            return status;
        }
    }
}
